<?php
/**
 * Created by PhpStorm.
 * User: Raymond
 * Date: 2021-07-16
 * Time: 09:33
 */

namespace ood\LMS;


class Member extends Account
{
    const MAX_BOOKS_ISSUED_TO_A_USER = 5;
    private $dateOfMembership;
    private $totalBooksCheckedout;

    public function getTotalBooksCheckedout() : int {}

    public function reserveBookItem(BookItem $bookItem) : bool {}

    private function incrementTotalBooksCheckedout() : void {}

    public function checkoutBookItem(BookItem $bookItem) {
        if ($this->getTotalBooksCheckedOut() >= self::MAX_BOOKS_ISSUED_TO_A_USER) {
            echo ("The user has already checked-out maximum number of books");
            return false;
        }
        $bookReservation = BookReservation::fetchReservationDetails($bookItem->getBarcode());
        if ($bookReservation != null && $bookReservation->getMemberId() != $this->getId()) {
            // book item has a pending reservation from another user
            echo ("This book is reserved by another member");
            return false;
        } else if ($bookReservation != null) {
            // book item has a pending reservation from the give member, update it
            $bookReservation->updateStatus(BookReservation::STATUS_COMPLETED);
        }

        if (!$bookItem->checkout($this->getId())) {
            return false;
        }

        $this->incrementTotalBooksCheckedout();
        return true;
    }

    private function checkForFine($bookItemBarcode) {
        $bookLending = BookLending::fetchLendingDetails($bookItemBarcode);
        $dueDate = $bookLending->getDueDate();
        $today = date("Y-m-d");
        // check if the book has been returned within the due date
        $datetime_start = new DateTime($dueDate);
        $datetime_end = new DateTime($dueDate);
        $diffDays = $datetime_start->diff($datetime_end)->days;
        if ($diffDays > 0) {
            Fine::collectFine($this->memberId, $diffDays);
        }
    }

  public function returnBookItem(BookItem $bookItem) {
    $this->checkForFine($bookItem->getBarcode());
    $bookReservation = BookReservation::fetchReservationDetails($bookItem->getBarcode());
    if ($bookReservation != null) {
        // book item has a pending reservation
        $bookItem->updateBookItemStatus(Book::STATUS_RESERVED);
        $bookReservation->sendBookAvailableNotification();
    }
    $bookItem->updateBookItemStatus(Book::STATUS_AVAILABLE);
  }

  public function renewBookItem(BookItem $bookItem) {
    $this->checkForFine($bookItem->getBarcode());
    $bookReservation = BookReservation::fetchReservationDetails($bookItem->getBarcode());
    // check if this book item has a pending reservation from another member
    if ($bookReservation != null && $bookReservation->getMemberId() != $this->getMemberId()) {
        echo ("This book is reserved by another member");
        $this->decrementTotalBooksCheckedout();
        $bookItem->updateBookItemState(Book::STATUS_RESERVED);
        $bookReservation->sendBookAvailableNotification();
        return false;
    } else if ($bookReservation != null) {
        // book item has a pending reservation from this member
        $bookReservation->updateStatus(BookReservation::STATUS_COMPLETED);
    }
    BookLending::lendBook($bookItem->getBarCode(), $this->getMemberId());
    $bookItem->updateDueDate();
    return true;
  }
}